package cn.hzw.supermarket.activity;

import static cn.hzw.supermarket.util.HttpUtil.sendHttpRequest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hzw.supermarket.R;
import cn.hzw.supermarket.adapter.RecyclerViewAdapter;
import cn.hzw.supermarket.databinding.ActivityBreadBinding;
import cn.hzw.supermarket.databinding.ActivityFruitBinding;
import cn.hzw.supermarket.domain.Goods;

public class BreadActivity extends AppCompatActivity {
    private ActivityBreadBinding binding;
    public static List<Goods> goodsList = new ArrayList<>();
    Goods goods = new Goods();
    RecyclerViewAdapter recyclerViewAdapter;
    Context context;
    //    RecyclerView goodsOne;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏以及状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        /**标题是属于View的，所以窗口所有的修饰部分被隐藏后标题依然有效,需要去掉标题**/
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityBreadBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        context = this;
        binding.layoutTitle.tvTitle.setText("商品页面");
        binding.layoutTitle.ivBack.setOnClickListener(view -> {
            finish();
        });

        binding.lvList.setLayoutManager(new LinearLayoutManager(this));
        goods.setGoodscategory(6);
        Log.d("TAG321", "onCreate: "+goods.getGoodscategory());
        postRequest("/goodsinfo/category", goods.getGoodscategory());
    }

    private void postRequest(String url, Integer category) {
        new Thread(() -> {
            Map<String, String> map = new HashMap<>();
            map.put("goodsCategory", String.valueOf(category));
            String response = sendHttpRequest(url, map);
            Log.d("TAG321", "postRequest: "+response);
            goodsList = JSONObject.parseObject(response,new TypeReference<List<Goods>>(){}.getType());//将JSON字符串转换成对象
            Log.d("TAG4", "postRequest: "+ goodsList.get(6));

            runOnUiThread(() -> binding.lvList.setAdapter(getAdapter(JSONObject.parseObject(response,new TypeReference<List<Goods>>(){}.getType()))));
        }).start();
    }

    private RecyclerViewAdapter<Goods> getAdapter(List<Goods> infos){
        return new RecyclerViewAdapter<Goods>(this, R.layout.goodslist1, infos) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.goodslist1;

            }

            @Override
            public void convert(ViewHolder holder, Goods data, int position) {
                holder.setImage(R.id.goods_pic, data.getGoodspicture());
                holder.setText(R.id.goods_name, data.getGoodsname());

                String priceText = "￥"+data.getGoodsprice()+"/"+data.getGoodsunit();
                holder.setText(R.id.goods_price, priceText);
                holder.setOnClickListener(R.id.goods, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
//                        Bitmap bitmap = setImage((data.getGoodsPicture()));
                        Bundle bundle = new Bundle();
                        bundle.putInt("id",data.getGoodsid());
                        Log.d("TAG5", "onClick: "+data.getGoodspicture());
                        bundle.putString("pic", data.getGoodspicture());
                        bundle.putString("name",data.getGoodsname());
                        bundle.putString("price","￥"+data.getGoodsprice()+"/"+data.getGoodsunit());
                        bundle.putString("content",data.getGoodscontent());
                        Intent intent = new Intent(BreadActivity.this,DetailsActivity.class);
                        intent.putExtras(bundle);
                        startActivity(intent);

                    }
                });

            }

        };
    }
}